package agend.client.agents;

import com.google.gdata.data.calendar.CalendarEventEntry;

import jade.gui.GuiAgent;
import jade.gui.GuiEvent;
import agend.client.HandlerJade;
import agend.client.behaviours.DeleteAppointmentBehaviour;
import agend.client.behaviours.RegistrationControlBehaviour;
import agend.client.behaviours.RequestAppointmentBehaviour;
import agend.client.behaviours.SyncAvailabilityWithAgentServer;
import agend.client.behaviours.SyncEmailBehaviour;
import agend.client.gui.utils.AgentInterface;
import agend.client.gui.utils.HandlerEvent;
import agend.client.gui.utils.HandlerView;


/**
 * Agente alla base del client
 */
public class AgentClient extends GuiAgent implements AgentInterface
{

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;

	public AgentClient() {
		// TODO Auto-generated constructor stub
		HandlerView.setAgent(this);	
		HandlerView.setLoginPanel();
		HandlerView.setVisible(true);
	}

	@Override
	protected void setup()
	{
		super.setup();
		System.out.println("okok");
		System.out.println("Agent "+getName()+" started");
	}

	@Override
	protected void onGuiEvent(GuiEvent evt) {

		switch (evt.getType()) {

		case HandlerEvent.REQUEST_APPOINTMENT:
		{
			addBehaviour(new RequestAppointmentBehaviour(evt));
			break;
		}
		case HandlerEvent.REGISTRATION_EVENT:
		{	
			RegistrationControlBehaviour registrationControlBehaviour = new RegistrationControlBehaviour();
			registrationControlBehaviour.setUsername(evt.getParameter(0).toString());
			registrationControlBehaviour.setPassword(evt.getParameter(1).toString());
			addBehaviour(registrationControlBehaviour);
			break;
		}
		case HandlerEvent.SYNC_EMAIL:
		{	
			//*******************************************************		
			addBehaviour(new SyncEmailBehaviour()); 
			//*******************************************************

			break;
		}
		case HandlerEvent.SYNC_AVAILABILITY_FROM_AGENT_SERVER:
		{
			addBehaviour(new SyncAvailabilityWithAgentServer(evt));
			break;

		}
		case HandlerEvent.DELETE_EVENT:
		{
			addBehaviour(new DeleteAppointmentBehaviour((CalendarEventEntry)evt.getParameter(0)));
			break;

		}
		default:
			break;

		}
	}


	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void createAgent(String username, String password) {
		// TODO Auto-generated method stub
		HandlerJade.start(username);

	}

	@Override
	public void shutDown() {
		// TODO Auto-generated method stub
		HandlerView.dispose();
		HandlerJade.shutDown();
	}
}
